package com.dcbeida.activity.stockdetail;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.SocketDetailAdapter;
import com.dcbeida.bean.Drug;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.StockDetail;
import com.dcbeida.widget.DataLoadDirection;
import com.dcbeida.widget.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 库存详情
 * Created by swt on 2017/9/29.
 */

public class StockDetailActivity extends BaseActivity implements XListView.IXListViewListener, StockDetailContract.View, AdapterView.OnItemClickListener {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.tv_kc)
    TextView tvKc;
    @Bind(R.id.tv_tx)
    TextView tvTx;
    @Bind(R.id.myList)
    XListView myList;

    private ArrayList<Object> datalist;
    private SocketDetailAdapter adapter;
    private StockDetailPersentr mPresent;

    private boolean isEdit = false;
    private boolean isEnd = false;// 是否有更多
    private int Totle = 0;//
    private int pageindex = 0;
    private int pagesize = 100;
    int type = DataLoadDirection.Refresh;
    boolean isHave = true; // 每个drugid下面的 库存是否有更多
    private String isall = "1";//0 只显示在库存中的数据 1所有的记录

    private int drugPos = 0;//药品的pos;

    @Override
    protected int getLayoutResId() {
        return R.layout.stock_detail;
    }

    @Override
    public void setData() {
        super.setData();
        tvTitle.setText(R.string.kcxq);
        datalist = new ArrayList<Object>();
        myList.setPullLoadEnable(false);
        myList.setPullRefreshEnable(true);
        myList.setXListViewListener(this);
        myList.setOnItemClickListener(this);
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        adapter = new SocketDetailAdapter(this);
        toHeaderData();
        myList.setAdapter(adapter);
        mPresent = new StockDetailPersentr(this, this);

        //mPresent.getList(isall, pageindex, pagesize, "");
        haveData(pageindex);
    }


    @OnClick(R.id.img_back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }

    @Override
    public void showTelTip(String msg) {
        showTip(msg);
    }

    @Override
    public void showView(boolean hasData) {

    }

    @Override
    public void showList(ArrayList<StockDetail.ListBean> list) {
        myList.hideFooterView();
        myList.setPullLoadEnable(false);
        if (type == DataLoadDirection.Refresh) {
            if (list.size() > 0) {
                list.get(0).setFirst(true);
                list.get(0).setCount(count);
                list.get(0).setDrugname(MyApplication.getInstance().getmDrugBean().getList().get(drugPos).getDrugname());
            }
            myList.stopRefresh();
            pageindex = 0;
            adapter.clearList();
        } else {
            if (list.size() > 0) {
                list.get(0).setFirst(true);
                list.get(0).setCount(count);
                list.get(0).setDrugname(MyApplication.getInstance().getmDrugBean().getList().get(drugPos).getDrugname());
            }
        }
        adapter.setmArrayList(list);
    }

    List<Integer> integerList = new ArrayList<>();

    @Override
    public void showData(StockDetail stockDetail) {
        String drugName = MyApplication.getInstance().getmDrugBean().getList().get(drugPos).getDrugname();
        int drogId = MyApplication.getInstance().getmDrugBean().getList().get(drugPos).getDrugid();




        if (!integerList.contains(drogId)) {
            if (integerList.size() != 0) {
                LayoutInflater.from(mContext).inflate(R.layout.stock_detail_header_line, headerParent, true);
            }
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.stock_detail_header_item, null);
            headerParent.addView(itemView);
            TextView tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            TextView tv_count = (TextView) itemView.findViewById(R.id.tv_count);
            TextView tv_day = (TextView) itemView.findViewById(R.id.tv_day);

            tv_name.setText(drugName);
            tv_count.setText(stockDetail.getStock() + "");
            tv_day.setText(stockDetail.getDaysforsale() + "");
            integerList.add(drogId);
        }
        type = DataLoadDirection.LoadMore;
        pageindex++;
        haveData(pageindex);
    }


    int count;

    private void haveData(int pageindex) {
        DrugListBean listBean = MyApplication.getInstance().getmDrugBean();
        if (listBean == null || listBean.getList() == null)
            return;
//        if (drugPos < listBean.getList().size()) {
//            if (drugPos + 1 >= listBean.getList().size()) {
//                isHave = false;
//            }

//            Log.i("info", "haveData: " + count);
//            mPresent.getList(isall, pageindex, pagesize, "", listBean.getList().get(drugPos).getDrugid());
//        }


        drugPos = pageindex;
        if (drugPos < listBean.getList().size()) {
            count = listBean.getList().get(drugPos).getStock();
            mPresent.getList(isall, 0, pagesize, "", listBean.getList().get(drugPos).getDrugid(),false);
        }


    }

    @Override
    public void onRefresh() {
        // 执行刷新任务
        drugPos = 0;
        type = DataLoadDirection.Refresh;
        haveData(0);
    }

    @Override
    public void onLoadMore() {
        // 执行加载更多任务
//        type = DataLoadDirection.LoadMore;
//        if (isEnd || isHave) {
//            if (!isEnd) {
//                haveData(0);
//            } else {
//                haveData(pageindex + 1);
//            }
//            // mPresent.getList(isall, pageindex + 1, pagesize, "");
//
//        }
    }

    LinearLayout headerParent;

    @Override
    public void toHeaderData() {
        headerParent = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.stock_detail_header, null);
        myList.addHeaderView(headerParent);
    }

    @Override
    public void showCount(int stock) {
        count =stock;
    }


    @Override
    public void setPresenter(StockDetailContract.Persent presenter) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        StockDetail.ListBean bean = (StockDetail.ListBean) adapter.getItem(position - 2);
        Bundle bundle = new Bundle();
        bundle.putSerializable("StockDetail.ListBean", bean);
        showActivity(JgmMxActivity.class, bundle);
    }
}
